The following options are used for compensating for inhomogeneities in the magnetic field. Shim sheets made of ferromagnetic material are installed at suitable sites, which is known as passive shimming. Gradient offsets (corresponding to linear shim terms) are used. Global shim coils are built immovably into the region of the gradient coils. Local shim coils are positioned close to the object under investigation. With the local shim coils close to the object under investigation, strongly localized inhomogeneities of the B0 field in the region of 0 to 10 ppm may be compensated for. Even field distortions caused by the patient with his/her residual susceptibility may be compensated for with the local shim coil. In addition, remaining inhomogeneities of the magnet that cannot be compensated for by passive shimming and the gradient offset may be compensated for with the local shim coil.